Repository navigation
C++: suggest specifiers for simple integer format mismatches - #23108
SkyCipherr wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The production-query change lacks the repository’s broader DCA evaluation to check for unexpected diagnostic regressions beyond the focused tests and MinGW database.
1 open issue
What changed in this PR
Adds format-specifier suggestions to the C++ cpp/wrong-type-format-argument query, partially addressing #15028 without changing mismatch detection.
Changes:
- Suggests matching specifiers for plain decimal integer conversions using promoted argument types.
- Adds Linux and Microsoft target-model tests covering suggestions and excluded cases.
- Updates existing expected diagnostics and adds a release note.
| File | Description |
|---|---|
cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft/WrongTypeFormatArguments.expected |
Updates Microsoft diagnostic expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Microsoft_no_wchar/WrongTypeFormatArguments.expected |
Updates no-wchar diagnostic expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_unsigned_chars/WrongTypeFormatArguments.expected |
Updates unsigned-char diagnostic expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Linux_signed_chars/WrongTypeFormatArguments.expected |
Updates signed-char diagnostic expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/WrongTypeFormatArguments/Buildless/WrongTypeFormatArguments.expected |
Updates buildless diagnostic expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.qlref |
Connects Microsoft tests to the query. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/WrongTypeFormatArguments.expected |
Records Microsoft suggestion expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/options |
Configures the Microsoft target model. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Microsoft/integer-hints.c |
Adds Microsoft suggestion and exclusion cases. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.qlref |
Connects Linux tests to the query. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/WrongTypeFormatArguments.expected |
Records Linux suggestion expectations. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/options |
Configures the Linux test extractor. |
cpp/ql/test/query-tests/Likely Bugs/Format/IntegerFormatSuggestions/Linux/integer-hints.c |
Adds Linux suggestion and exclusion cases. |
cpp/ql/src/Likely Bugs/Format/WrongTypeFormatArguments.ql |
Appends suggestions for supported integer mismatches. |
cpp/ql/src/change-notes/2026-11-12-integer-format-suggestions.md |
Documents the diagnostic enhancement. |
Review effort: Balanced (auto)
| select arg, | ||
| "This format specifier for type '" + expected.getName() + "' does not match the argument type '" + | ||
| actual.getUnspecifiedType().getName() + "'." | ||
| actual.getUnspecifiedType().getName() + "'." + integerFormatSuggestion(ffc, n, arg, actual) |
|
This feels very hacky. I think a more principled approach would likely be to update |
|
Thanks @jketema. Following your suggestion, I've reworked this in 0dc2c48.
The API is deliberately limited to the existing scope: plain decimal Validation with CodeQL CLI 2.27.4:
These are local validation results. I have not run the full repository suite or refreshed upstream DCA for this commit; it still needs upstream CI/DCA. |
|
You misunderstood me: this will need to be properly integrated with what is in |
Use the existing length and signedness model for both conversion type inference and reverse specifier suggestions. Preserve narrow display types, target-specific extensions, and the existing suggestion scope. Add Linux and Microsoft regression coverage recorded against the prior model. All 26 printf library and format query tests pass; real-GCC messages and locations remain unchanged.
|
Thanks for clarifying. I misunderstood your earlier suggestion: the previous revision still maintained a separate type-to-specifier mapping, even though it was placed in I've reworked this in 30acf5c9 so that type inference and suggestions now share the existing model:
The query’s detection predicates and the supported suggestion scope remain unchanged. Validation with CodeQL CLI 2.27.4:
These are local results; I have not run the full repository suite or upstream DCA. |
|
We're moving in a direction where the code complexity is increasing and this is starting to become unmaintainable. Also |
|
Thanks for clarifying. I restored the original display-type implementation in All 24 retained tests passed locally. Re-running the query on the MinGW-w64 GCC database produced the same 19 alerts and 10 suggestions, with identical messages and locations. Broader DCA is still pending. |
|
We don't seem to be making a lot of progress here. And we seem almost back in the first version of this. This seems to be partially due to the weird decision that was made to only look at a few specific specifiers. |

Summary
Related to #15028.
Add matching format-specifier suggestions to existing
cpp/wrong-type-format-argumentalerts for simple decimal integer conversions. For example, a%dconversion with anunsigned longargument on LP64 suggests%lu.Printf.qllexposes its existing integer conversion rules throughFormatLiteral.getIntegerConversionType(length, conversion). BothgetConversionTypeand the query use this relation, which applies the existing length and signedness rules for the target platform.getIntegralDisplayTypeuses its original implementation.The query uses
parseConvSpecto identify eligible conversions and selects a candidate whose required type matches the promoted argument’s canonical arithmetic type. Suggestions cover plaind,i, anduconversions with optionallorllfor signed and unsignedint,long, andlong long. The query preservesifor signed suggestions and adds the message only to existing alerts on conversion-value arguments.Validation
Local validation with CodeQL CLI 2.27.4 used base commit
fbf9fca5ce7c3b64c2f5d719ea0444577850cb4a.git diff --checkpassed.Upstream CI and broader DCA remain pending.